// packageCommunity/pages/follow/index.js
import{comments} from "../../../js/modules/mall.js" 
import BasePage from '../../../js/common/base-page' 
import { util,api } from "../../../js/util.js";
var app = getApp();
BasePage({

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    videoDomain:api.videoDomain,
    followList:[],//列表
    PageIndex:1,//页数
    PageSize:5,//条数
    hasMoreData:true,
    statusId:0,
    follow:{
      Proprietor:'',
      ProprietorId:'',
      FKFlag:'',
      FKId:'',
      UserId:'',
      UserName:'',
    },
    count:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {follow} = this.data;
    follow.Proprietor = options.proprietor;
    follow.ProprietorId = options.mainid;
    follow.FKFlag = options.fkflag;
    follow.FKId = options.fkid;
    follow.UserId = options.userid;
    follow.UserName = options.username;
    // console.log(follow,'--------fol');
    wx.setNavigationBarTitle({
      title: options.id==0?'关注':'粉丝',
    })
    this.setData({
      statusId:options.id,
      follow:follow
    })
    //关注列表
    this.getDataList();
  },
  getDataList:function(){
    var that = this;
    var {follow} = that.data;
    var data = {
      Proprietor:follow.Proprietor,
      ProprietorId:follow.ProprietorId,
      FKFlag:follow.FKFlag,
      FKId:follow.FKId,
      UserId:follow.UserId,
      UserName:follow.UserName,
      PageIndex:that.data.PageIndex,
      PageSize:that.data.PageSize,
      statusId:that.data.statusId
    }
    // console.log(data,'-----信息');
    comments.getFollowList(data,function(content){
      let hasMore = content.Content.count > that.data.PageIndex * that.data.PageSize;
      var data = content.Content.data;
      data.forEach(function(item) {
        item.IsFollow = item.IsFollow == 0?true:false;
      })
      //当分页大于1时，累加
      if (that.data.PageIndex > 1) {
        data = that.data.followList.concat(data);
      }
      that.setData({
        followList:data,
        hasMoreData: hasMore,
        count:content.Content.count
      })
    })
  },
  cancleAttention(e){
    var that = this;
    var item = e.currentTarget.dataset.item;
    var follow = e.currentTarget.dataset.follow;
    var data = {
      FollowFkFlag:that.data.statusId ==0?item.FollowFKFlag:item.FKFlag,
      FollowFkId:that.data.statusId ==0?item.FollowFKId:item.FKId,
      FollowManagerId:that.data.statusId ==0?item.FollowManagerId:item.ManagerId
    }
    if(follow){//关注==取消关注
      comments.getUserFollowAdd(data,function(res){
        if(res.Success){
          that.checkList(that.data.statusId ==0?item.FollowManagerId:item.ManagerId);
          util.tips('关注成功');
        }else{
          util.tips(res.Message);
        }
      })
    }else{
      comments.getUserCancelFollow(data,function(res){
        if(res.Success){
          that.checkList(that.data.statusId ==0?item.FollowManagerId:item.ManagerId);
          util.tips('取消关注成功');
        }else{
          util.tips(res.Message);
        }
      })
    }
  },
  checkList(FollowManagerId){
    var that = this;
    var list = that.data.followList;
    list.map(function(item){
      if(that.data.statusId ==0?item.FollowManagerId:item.ManagerId == FollowManagerId){
        item.IsFollow = !item.IsFollow
      }
    })
    that.setData({
      followList:list
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      var index = this.data.PageIndex;
      this.setData({
        PageIndex:index+1
      },function(){
        this.getDataList();
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})